﻿using System;
using System.Diagnostics;
using System.IO;
using System.Security.Principal;
using System.Text;
using System.Windows.Forms;

namespace Rf_Regasm
{
    public partial class Form1 : Form
    {
        //组件文件
        string WpcFile = null;

        //注册文件
        string RegasmFile = null;

        //安装目录
        string SavePath = "C:/Windows/Temp";

        public Form1()
        {
            InitializeComponent();
        }

        //Form初始化
        private void Form1_Load(object sender, EventArgs e)
        {
            //初始化组件文件位置
            this.WpcFile = Application.StartupPath +Path.DirectorySeparatorChar + "Wpc.mdb";
            if (!System.IO.File.Exists(this.WpcFile)) 
            {
                MessageBox.Show("Wpc文件缺失");
                Application.Exit();
            }

            //初始化注册文件位置
            string RuntimePath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
            this.RegasmFile = RuntimePath + "RegAsm.exe";

            //32位平台支持代码
            //this.RegasmFile = this.RegasmFile.Replace("Framework64", "Framework");
            if (!System.IO.File.Exists(this.RegasmFile))
            {
                MessageBox.Show("Regsvr32文件缺失");
                Application.Exit();
            }

            //检查管理员权限
            bool isAdmin = CheckIsAdmin();
            if (!isAdmin)
            {
                MessageBox.Show("请使用管理员权限启动程序！");
                Application.Exit();
            }
        }

        //Regasm注册
        protected bool Regasm(string RegasmFile, string wpcFile)
        {
            try
            {
                //组装执行命令
                StringBuilder cmdsb = new StringBuilder();
                cmdsb.Append(@RegasmFile);
                cmdsb.Append(@" /codebase ");
                cmdsb.Append(@wpcFile);
                cmdsb.Append(" &exit");

                //创建进程执行
                Process pro = new Process();
                pro.StartInfo.FileName = "cmd.exe";
                pro.StartInfo.UseShellExecute = false;
                pro.StartInfo.RedirectStandardInput = true;
                pro.StartInfo.RedirectStandardOutput = true;
                pro.StartInfo.RedirectStandardError = true;
                pro.StartInfo.CreateNoWindow = false;
                pro.Start();
                pro.StandardInput.WriteLine(cmdsb.ToString());
                pro.StandardInput.AutoFlush = true;
                pro.WaitForExit();

                //获取执行结果
                string output = pro.StandardOutput.ReadToEnd();
                pro.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }

            return true;
        }

        //安装
        private void Install_Click(object sender, EventArgs e)
        {
            //01.检查文件
            if (this.SavePath == null) 
            {
                MessageBox.Show("请配置安装目录");
                return;
            }
            if (!Directory.Exists(this.SavePath))
            {
                Directory.CreateDirectory(this.SavePath);
                if (!Directory.Exists(this.SavePath))
                {
                    MessageBox.Show("创建配置安装目录失败");
                    return;
                }
                return;
            }

            //02.移动组件文件到安装目录
            string NewWpcFile = SavePath + Path.DirectorySeparatorChar + "Wpc.dll";
            File.Copy(this.WpcFile, NewWpcFile,true);
            if (!File.Exists(NewWpcFile)) 
            {
                MessageBox.Show("下载Wpc.dll文件到安装目录失败");
                return;
            }

            //03.注册组件文件
            bool install  = Regasm(RegasmFile, NewWpcFile);
            if (install)
            {
                this.install.Text = "已执行成功";
                this.install.Enabled = false;
            }
            else 
            {
                this.install.Text = "已执行失败";
                this.install.Enabled = false;
            }
        }

        //检查是否超级管理员
        static bool CheckIsAdmin()
        {
            WindowsPrincipal windowsPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
        }
    }
}
